package com.tanhua.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.pojo.Comment;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.server.pojo.User;
import com.tanhua.server.pojo.UserInfo;
import com.tanhua.server.utils.UserThreadLocal;
import com.tanhua.server.vo.Comments;
import com.tanhua.server.vo.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.AccessType;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author: JinboYang
 * @Description:
 * @Date: Created in 下午 14:20 2021/1/8
 * @ModifiedBy:
 */
@Service
public class CommentsService {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Reference(version = "1.0.0")
    private QuanZiApi quanZiApi;

    public PageResult queryCommentsList(String publishId, Integer page, Integer pageSize) {
        //根据publishId查询出所有的评论信息
        PageInfo<Comment> commentPageInfo = this.quanZiApi.queryCommentList(publishId, page, pageSize);

        PageResult pageResult = new PageResult();
        pageResult.setPagesize(pageSize);
        pageResult.setPage(page);
        pageResult.setPages(0);

        List<Comment> records = commentPageInfo.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return pageResult;
        }

        //获取所有评论用户的id，用set取，防止id重复
        Set<Long> userIds = new HashSet();
        for (Comment record : records) {
            userIds.add(record.getUserId());
        }


        //mysql里面查询到所有userInfo
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper();
        queryWrapper.in("user_id", userIds);

        List<UserInfo> userInfoList = this.userInfoService.queryUserInfoList(queryWrapper);

        List<Comments> commentsList = new ArrayList<>();
        //查询userInfo数据
        for (Comment record : records) {
            Comments comments = new Comments();

            comments.setId(record.getId().toHexString());
            comments.setCreateDate(new DateTime(record.getCreated()).toString("yyyy年MM月dd日 HH:mm"));
            comments.setContent(record.getContent());

            for (UserInfo userInfo : userInfoList) {
                if (record.getUserId().longValue() == userInfo.getUserId().longValue()) {
                    comments.setNickname(userInfo.getNickName());
                    comments.setAvatar(userInfo.getLogo());
                    break;
                }
            }

            String likeUserCommentKey = "QUANZI_COMMENT_LIKE_USER_" + UserThreadLocal.get().getId() + "_" + comments.getId();
            comments.setHasLiked(this.redisTemplate.hasKey(likeUserCommentKey) ? 1 : 0); //是否点赞

            String likeCommentKey = "QUANZI_COMMENT_LIKE_" + comments.getId();
            String value = this.redisTemplate.opsForValue().get(likeCommentKey);
            if (StringUtils.isNotEmpty(value)) {
                comments.setLikeCount(Integer.valueOf(value)); //点赞数
            } else {
                comments.setLikeCount(0); //点赞数
            }

            commentsList.add(comments);
        }

        pageResult.setItems(commentsList);

        return pageResult;
    }

    /**
     * @Description: 保存评论
     * @Param:[published, comment]
     * @return:java.lang.Boolean
     * @Author:JinboYang
     * @Date:2021/1/8
     */
    public Boolean saveComments(String published, String comment) {
        return this.quanZiApi.saveComment(UserThreadLocal.get().getId(), published, 2, comment);
    }
}
